﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils.Images;
using Framework.Library.Core.Config;

namespace Framework.Portal.Layouts.Client.Components.Teenio
{
    public partial class Categories : BaseControl
    {
        public string MENUNAME = string.Empty;

        public Categories()
        {
        }

        public Categories(JSection section)
        {
            if (!string.IsNullOrEmpty(section.parameter))
            {
                string[] arrParams = section.parameter.Split('&');
                if (arrParams.Length > 0)
                    menuId = Convert.ToInt32(arrParams[0].Split('=')[1]);
                if (arrParams.Length > 1)
                    catId = Convert.ToInt32(arrParams[1].Split('=')[1]);
            }
            MENUNAME = section.title;
        }
        protected static int catId = 0;
        protected static int menuId = 0;

        protected string arrayArticle = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int curPage = 0;
                if (Request.QueryString["page"] != null)
                    int.TryParse(Request.QueryString["page"].ToString(), out curPage);
                curPage--;
                if (curPage < 0)
                    curPage = 0;
                int totalRecord;
                List<TeenioContent> lstContent = ManagerFactory.TeenioContentManager.GetList(catId, 0, 2, 0, curPage, Constants.PAGESIZE, out totalRecord);
                if (lstContent != null)
                {
                    rptContent.DataSource = lstContent;
                    rptContent.DataBind();
                    arrayArticle = "[" + arrayArticle.TrimEnd(',') + "]";
                    if (totalRecord <= Constants.PAGESIZE)
                        lblPaging.Visible = false;
                    else
                    {
                        curPage++;
                        lblPaging.Text = Paging.getPaging(Request.RawUrl, 5, "page", curPage, (int)Math.Ceiling((double)totalRecord / Constants.PAGESIZE));
                    }
                }
            }
        }

        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (TeenioContent)e.Item.DataItem;
                var imgAvatar = (Image)e.Item.FindControl("imgAvatar");
                var hplTitle = (HyperLink)e.Item.FindControl("hplTitle");
                var lblView = (Literal)e.Item.FindControl("lblView");
                var lblTeaser = (Literal)e.Item.FindControl("lblTeaser");
                var lblCreateDate = (Literal)e.Item.FindControl("lblCreateDate");

                if (!string.IsNullOrEmpty(info.CONTENT_AVATAR))
                    imgAvatar.ImageUrl = ImagesUtils.GetImage(info.CONTENT_AVATAR, 110, 90);
                hplTitle.Text = info.CONTENT_HEADLINE;
                hplTitle.NavigateUrl = cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, info.CONTENT_ID);
                lblCreateDate.Text = DateTimeHelper.formatDateTime(info.CONTENT_MODIFYDDATE);
                lblTeaser.Text = ConvertUtility.ToSubString(info.CONTENT_TEASER, 260);

                arrayArticle += info.CONTENT_ID + ",";
            }
        }
    }
}